package com.example.menhoo.toollibrary.common.helper;

import android.app.Activity;
import android.app.Application;

import com.example.menhoo.toollibrary.android.log.Log;

import java.util.LinkedList;
import java.util.List;

/**
 *退出这个App的类
 * onCreate()方法中调
 用ExitAppHelper.getInstance().addActivity(Activity activity)方法如:ExitAppHelper.getInstance().addActivity(this);。在任何一个Activity 界面退
 出应用程序时，只要调用ExitAppHelper.getInstance().exit()方法，就可以在任何一个Activity
 中完全退出应用程序。

 * */
public class ExitAppUtil extends Application {

    //存放退出整个应用的Activity的list
    private List<Activity> activityList = new LinkedList<Activity>();
    //存放退出多个Activity的list不退出应用
    private List<Activity> activityListTemp = new LinkedList<Activity>();
    private static ExitAppUtil instance;

    private ExitAppUtil() {
    }

    // 单例模式中获取唯一的ExitApplication 实例
    public static ExitAppUtil getInstance() {
        if (null == instance) {
            instance = new ExitAppUtil();
        }
        return instance;

    }

    // 添加Activity 到容器中
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }
    // 添加Activity 到容器中
    public void addTempActivity(Activity activity) {
        activityListTemp.add(activity);
    }

	/**
	 * 退出整个应用
     * 遍历所有Activity 并finish
	 * */
    public void exit() {

        for (Activity activity : activityList) {
            try {
                activity.finish();
            } catch (Exception e) {
                Log.e(e.getMessage());
            }

        }

//        System.exit(0);

    }
    /**
     * 只退出 所以保存的 Activity  不退出应用
     */
    public void exitAllActivity() {

        for (Activity activity : activityList) {
            try {
                activity.finish();
            } catch (Exception e) {
                Log.e(e.getMessage());
            }
        }
    }
    public void exitTempActivity() {

        for (Activity activity : activityListTemp) {
            try {
                activity.finish();
            } catch (Exception e) {
                Log.e(e.getMessage());
            }
        }
    }
}
